package com.oliver.proxy.midjourney.infrastructure.mapper;

import com.oliver.proxy.midjourney.domain.entity.Prod;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.Select;
import java.util.List;
import java.util.Map;

@Mapper
public interface ProdMapper {
    void insert(Prod prod);
    void batchInsert(@Param("prods") List<Prod> prods);
    void batchUpdate(@Param("prods") List<Prod> prods);
    void batchUpdateImages(@Param("prods") List<Prod> prods);
    void batchUpdateImagesWithPath(@Param("prods") List<Prod> prods);
    List<Prod> selectByTaskId(@Param("taskId") Long taskId);
    List<Prod> selectUnhandledProds();
    List<Prod> selectUnprocessedImages();
    List<Prod> selectByMjTaskId(String mjTaskId);
    List<Prod> selectProdsWithTitle();
    void updateTitle(Prod prod);

    /**
     * 查找prod_type为0且attritube_6为空的记录
     */
    List<Prod> findProdsWithEmptyAttritube6AndType0();
    
    @Update("<script>" +
            "UPDATE prod SET prompt = #{prompt} " +
            "WHERE data_id IN " +
            "<foreach item='id' collection='prodIds' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    void updateBatchPrompt(@Param("prodIds") List<Long> prodIds, @Param("prompt") String prompt);
    @Select("SELECT p.data_id, p.task_id, p.prompt, t.task_category " +
            "FROM prod p " +
            "INNER JOIN task_list t ON p.task_id = t.task_id " +
            "WHERE p.attritube_5 IS NULL " +
            "and p.prompt IS NOT NULL " +
            "ORDER BY p.task_id, p.prompt")
    List<Map<String, Object>> selectProdsWithEmptyAttribute5();
    @Update("<script>" +
            "UPDATE prod SET attritube_5 = #{spuCode} " +
            "WHERE data_id IN " +
            "<foreach item='id' collection='prodIds' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    void updateBatchAttribute5(@Param("prodIds") List<Long> prodIds, @Param("spuCode") String spuCode);
    @Select("SELECT data_id, attritube_3, attritube_5 FROM prod " +
            "WHERE attritube_5 IS NOT NULL " +
            "AND attritube_3 IS NOT NULL " +
            "AND attritube_4 IS NULL ")
    List<Map<String, Object>> selectProdsWithAttribute5AndUnmovedFiles();
    @Update("UPDATE prod SET attritube_4 = 'MOVED' WHERE data_id = #{dataId}")
    void updateAttribute3AndMoveFlag(@Param("dataId") Long dataId, @Param("newPath") String newPath);
    @Select("SELECT SUBSTRING(attritube_5, -4) as max_seq " +
            "FROM prod " +
            "WHERE attritube_5 LIKE CONCAT(#{category}, #{devNumber}, '-', #{prototypeNumber}, #{dateStr}, '%') " +
            "ORDER BY attritube_5 DESC LIMIT 1")
    String getMaxSequenceNumber(@Param("category") String category,
                               @Param("devNumber") String devNumber,
                               @Param("prototypeNumber") String prototypeNumber,
                               @Param("dateStr") String dateStr);
}